<?php

namespace App\Http\Controllers;

use DB;
use Validator;
use App\Models\User;
use App\Models\Bank;
use App\Services\OSS;
use App\Models\Circle;
use App\Models\Extract;
use App\Models\MealShop;
use App\Models\MealCate;
use App\Models\MealSort;
use App\Models\MealOrder;
use App\Models\MealCompany;
use App\Exports\ShopExport;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Redis;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Maatwebsite\Excel\Mixins\DownloadCollection;

class MealShopController extends Controller
{
    public function createCate(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'cate_name'=>'required',
            'cate_desc'=>'max:150',
            'cate_price'=>'required|numeric',
        ],[
            'cate_name.required'=>'餐品名称不能为空！',
            'cate_desc.max'=>'餐品描述不可超过150字',
            'cate_price.required'=>'请填写价格',
            'cate_price.numeric'=>'价格请使用数字格式表示',
        ]);
        if($validator->fails()){
            $errors = $validator->errors()->all();
            if(count($errors)<=1){
                return response()->json(['success'=>false,'code'=>'003','message'=>$errors[0]]);
            }else{
                return response()->json(['success'=>false,'code'=>'003','message'=>$errors]);
            }
        }
        $post = $request->only([
            'cate_name', // 商品名称
            'cate_desc', // 商品描述
            'cate_price', // 商品价格
            'shop_id', // 商户ID
            'sort_id', // 分类ID
            'cate_id', // 餐品ID
            'cate_status', // 分类ID
        ]);
        
        $shopinfo = MealShop::where('shop_id',$post['shop_id'])->first();
        $u = User::where('user_id',$shopinfo->users_id)->select('reliability')->first();
        if($u['reliability']!=2){
            return response()->json(['success'=>false,'code'=>'016','message'=>'您当前未通过认证，不涉及操作此功能！']);
        }
        $dayTime = date('Ymd');
        // 图片上传到 OSS
        if($request->hasFile('cate_img')){
            $file = $request->file('cate_img');
            $fileExt = ltrim($file->getClientOriginalExtension(),'.');
            $fileName = md5(uniqid(microtime(true),true)).'.'.$fileExt;
            $filePath = 'MealCateImgs/'.$dayTime.'/';
            $upRes = OSS::publicUpload(config('aliyun.BucketName'),$filePath.$fileName,$file);
            if(!$upRes){
                return response()->json(['success'=>false,'code'=>'001','message'=>'图片上传失败！']);
            }
            $post['cate_img'] = '/'.$dayTime.'/'.$fileName;
        }
        
        $mealcate = new MealCate();
        $insertRes = $mealcate->createCate($post);
        if($insertRes){
            return response()->json(['success'=>true,'code'=>'000','message'=>$post['cate_name'].'已添加到餐品列表！']);
        }else{
            return response()->json(['success'=>false,'code'=>'002','message'=>'餐品添加失败，请稍后重试！']);
        }
    }

	/**
	 * 商铺详情
	 */
    public function details(Request $request) {

    	$shopid = $request->input('shopid');

        $shop = new MealShop;

        $order = new MealOrder;

    	$shops = $shop->leftJoin('lexiang_user','lexiang_meal_shop.users_id','=','lexiang_user.user_id')
        ->where('lexiang_meal_shop.shop_id',$shopid)
        ->select('lexiang_meal_shop.*','lexiang_user.account','lexiang_user.nicename')
        ->first();

        $extract = new Extract;

        $picpath = OSS::getPublicObjectURL(config('aliyun.BucketName'),'MealShopLogo');

        $shops['sell'] = self::daynowSell($shopid);

        $shops['ordertotal'] = $order->orderTotal($shopid);

        $shops['extractlist'] = $extract->shoplist($shops->users_id,8);

        $shops['shopcate'] = MealCate::shopCateList($shopid,8);

        $orders = $order->list($shopid,8);

        $extract_status_de = ['1'=>'待处理','2'=>'处理中','3'=>'已打款','4'=>'已驳回'];

        $order_status_de = ['-2'=>'已撤销','-1'=>'加价未支付','1'=>'已取餐','0'=>'未取餐','2'=>'已超时','3'=>"取消订单"];
 
        $cate_status_de = ['1'=>'出售中','0'=>'已下架',-1=>'已删除'];

        $extractMoney = $extract->userExtractAccount($shops->users_id);
        $sorts = MealSort::where('state',1)->get();
    	return view('shop.shopdetails',['shop'=>$shops,'picpath'=>$picpath,'extract_status_de'=>$extract_status_de,'orders'=>$orders,'order_status_de'=>$order_status_de,'extract_money'=>$extractMoney,'cate_status_de'=>$cate_status_de,'sorts'=>$sorts]);
    }

    /**
     * 商铺列表
     */
    public function shopList(Request $request) {

    	$shops = MealShop::leftJoin('lexiang_user','lexiang_user.user_id','=','lexiang_meal_shop.users_id')

        ->where(function($q)use($request){
            if(session('region') == 3){
                $q = $q->where('lexiang_meal_shop.company_id','=',session('company_id'));
            }
            if($request->input('company_id',false)){
                $q = $q->where('lexiang_meal_shop.company_id','=',$request->input('company_id',false));
            }
            if($request->input('shop_name',false)){
                $q = $q->where('lexiang_meal_shop.shop_name','like','%'.$request->input('shop_name').'%');
            }
            if($request->input('circle_id',false)){
                $q = $q->where('lexiang_meal_shop.circle_id','=',$request->input('circle_id'));
            }
            return $q;
        })

    	->select(DB::raw('lexiang_meal_shop.*,lexiang_user.nicename,lexiang_user.tel,lexiang_user.account,null as circle_name,null as shop_run_status_text'))

        ->orderBy('lexiang_meal_shop.insert_time','desc')
        
    	->paginate(15,['shop_name','circle_id','company_id']);

        $path = OSS::getPublicObjectURL(config('aliyun.BucketName'),'ShopQrcodeImg');

    	return view('shop.shoplist',['shops'=>$shops,'qrcodepath'=>$path]);
    }

    /**
     * 菜品列表
     */
    public function cateList(Request $request)
    {
        $shopid = $request->input('shops_id');
        $cate = new MealCate;
        $cates = $cate->leftJoin('lexiang_meal_sort','lexiang_meal_sort.sort_id','=','lexiang_meal_cate.sort_id')
        ->where([
            ['lexiang_meal_cate.shops_id','=',$shopid],
            ['lexiang_meal_cate.cate_status','=',1],
        ])
        ->orderBy('lexiang_meal_sort.sort_order','asc')
        ->orderBy('lexiang_meal_cate.cate_price','asc')
        ->paginate(10);
        $sorts = $cate->where([
            ['shops_id','=',$shopid],
            ['cate_status','=',1],
        ])->pluck('sort_id');
        $sort = new MealSort;
        $sort_cate = $sort->whereIn('sort_id',$sorts)->orderBy('sort_order','asc')->get();
        $shopinfo = MealShop::where('shop_id',$shopid)->selectRaw('shop_name,shop_id,circle_id,null as circle_name,packfood_if')->first();
        return response()->json([
            'success'=>true,
            'code'=>'000',
            'message'=>'查询完成',
            'data'=>$cates,
            'sort_cate'=>$sort_cate,
            'shop_name'=>$shopinfo->shop_name,
            'packfood_if'=>$shopinfo->packfood_if,
            'circle_name'=>$shopinfo->circle_name,
        ]);
    }


    /**
     * 饭店列表
     */
    public function shopLists(Request $request)
    {
        $lng = $request->input('lng',0);// shop_position_x 经度
        $lat = $request->input('lat',0);// shop_position_y 维度

        $shops = MealShop::leftJoin('lexiang_user','lexiang_user.user_id','=','lexiang_meal_shop.users_id')
        ->leftJoin('lexiang_circle','lexiang_circle.circle_id','=','lexiang_meal_shop.circle_id')
        ->where(function($q)use($request){
            if($request->input('shop_name',false)){
                return $q->where('lexiang_meal_shop.shop_name','like','%'.$request->input('shop_name').'%');
            }
        })
        ->where('shop_run_status',1)
        ->select(DB::raw('lexiang_meal_shop.*,(POWER(MOD(ABS(lexiang_meal_shop.shop_position_y - '.$lng.'),360),2) + POWER(ABS(lexiang_meal_shop.shop_position_x - '.$lat.'),2)) AS distance,null as distance_now,'.$lng.' as lng2,'.$lat.' as lat2,null as business_state,(POWER(MOD(ABS(lexiang_circle.lng - '.$lng.'),360),2) + POWER(ABS(lexiang_circle.lat - '.$lat.'),2)) AS distance_circle'))

        ->orderBy('distance_circle','asc')
        ->orderBy('circle_sort','asc')
        ->orderBy('distance','asc')
        ->paginate(10);
        if($shops){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'resultCode'=>1,
                'message'=>'查询成功',
                'data'=>$shops
            ]);
        }else{
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'查询成功',
                'data'=>$shops,
                'resultCode'=>0,
            ]);
        }
        
    }

    /**
     * 导出excel
     */
    public function dumpExcel(Request $request)
    {
        return Excel::download(new ShopExport($request->all()), date('Y m月d日').'导出商户数据.xlsx');
    }

    /**
     * 今日销量
     */
    public static function daynowSell($shopid)
    {
        $nowtime = strtotime(date('Y-m-d 00:00:00'));

    	return MealOrder::where('shops_id',$shopid)
        ->where('insert_time','>',$nowtime)
        ->where('order_status','>','0')
        ->count();
    }

    /**
     * 提现列表
     */
    public function extractList(Request $request)
    {
        $extract = new Extract;

        $exdata = $extract->shoplist();

        $status_de = ['1'=>'待审','2'=>'通过申请','3'=>'已打款','4'=>'已驳回'];

        if($request->ajax){
            return response()->json(['success'=>true,'code'=>'0','data'=>$exdata]);
        }else{
            return view('shop.extractlist',['exdata'=>$exdata,'status_de'=>$status_de]);
        }
    }

    /**
     * 提现处理操作
     */
    public function extractAction(Request $request)
    {
        $nowtime = time();

        $post = $request->only(['action','extractid','extract_desc']);

        if(empty($post['extractid'])) return response()->json(['success'=>false,'code'=>'1','message'=>'提现ID不能为空']);

        $action_de = ['YCL'=>3,'BH'=>4,'KS'=>2];

        $extract = Extract::find($post['extractid']);

        if(!$extract) return response()->json(['success'=>false,'code'=>'1','message'=>'未查询到']);

        switch ($post['action']) {
            case 'YCL':// 已打款
                $extract->extract_status = $action_de[$post['action']];
                break;
            case 'BH':// 驳回
                $extract->extract_status = $action_de[$post['action']];
                if(empty($post['extract_desc'])){
                    return response()->json(['success'=>false,'code'=>'1','message'=>'请填写理由']);
                }
                $extract->extract_explain = $post['extract_desc'];
                break;
            case 'KS':// 开始处理
                $extract->extract_status = $action_de[$post['action']];
                break;
        }

        $result = $extract->save();

        if($result){
            return response()->json(['success'=>true,'code'=>0,'message'=>'修改成功！']);
        }else{
            return response()->json(['success'=>false,'code'=>'2','message'=>'修改失败，请稍后重试！']);
        }
    }

    /**
     * 修改商铺营业状态
     */
    public function editStatus(Request $request)
    {
        $post = $request->only(['action','shopid']);

        $action_de = ['ZT'=>'-1','KQ'=>'1'];

        $actiontext_de = ['ZT'=>'关闭','KQ'=>'开启'];

        if(!isset($action_de[$post['action']])){
            return response()->json(['success'=>false,'code'=>'1','message'=>'不可识别的操作！']);
        }

        $shop = new MealShop;

        $res = $shop->controlSwitch($action_de[$post['action']],$post['shopid']);

        if($res){
            return response()->json(['success'=>true,'code'=>"0",'message'=>'您已将店铺'.$actiontext_de[$post['action']].'营业！']);
        }else{
            return response()->json(['success'=>false,'code'=>"2",'message'=>$actiontext_de[$post['action']].'失败，请联系开发人员处理！']);
        }
    }

    /**
     * 预生成二维码
     */
    public function createQrcode()
    {

        $shopidcreate = Redis::get('shopidcreate');

        $shopid = MealShop::orderBy('shop_id','desc')->select('shop_id')->first()['shop_id'];

        return view('shop.shopqrcode',['shopidcreate'=>$shopidcreate,'maxid'=>$shopid]);

    }

    /**
     * 创建二维码
     */
    public function actionCreatecode(Request $request)
    {

        $post = $request->only('shopid_lately');

        $id = trim($post['shopid_lately']) ?? 0;

        Redis::set('shopidcreate',$id);

        if(strtolower(substr(PHP_OS, 0, 3)) == 'win'){
            $pm = 'tmp\\';
        }else{
            $pm = 'tmp/';
        }

        $logo = '/public/assets/img/logo2.png';

        $filename = 'qrcode.png';

        QrCode::format('png')
        ->size(300)
        ->margin(1)
        ->merge($logo,.15)
        ->encoding('UTF-8')
        ->generate('https://api-shop.jiashilexiang.cn/?shop_id='.$id,$pm.$filename);
        
        return response()->json(['success'=>true,'code'=>'0','message'=>'成功','qrcode'=>'/'.rtrim($pm,"\\").'/'.$filename,'shopidcreate'=>$id]);

    }

    /**
     * 账号搜索店铺
     */
    public function searchTel(Request $request)
    {

        $tel = $request->input('tel',false);

        $shopid = $request->input('shop_id');

        if(empty($tel)){
            $field = 'shop_id';
            $value = $shopid;
        }else{
            $field = 'shop_tel';
            $value = $tel;
        }

        $shop = new MealShop;

        $data = $shop->retrieShop($field,trim($value),['shop_id','shop_name','shop_address']);

        if($data){
            return response()->json(['success'=>true,'code'=>'0','message'=>'成功','data'=>$data]);
        }else{
            return response()->json(['success'=>false,'code'=>'1','message'=>'未查询到该账号','data'=>[]]);
        }
        
    }

    /**
     * 修改餐品图片
     */
    public function editCateImg(Request $request) 
    {
        $nowtime = time();
        $cateid = $request->input('cates_id');
        $cateData = MealCate::where('cate_id',$cateid)->select('cate_imgs')->first();
        if(!$cateData){
            return response()->json(['success'=>false,'code'=>'1','message'=>"未查询到餐品！"]);
        }
        if($request->hasFile('cateimg')){
            $cateimg = $request->file('cateimg');
            $filename = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$cateimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'MealCateImgs'.$filename,$cateimg);
            OSS::publicDeleteObject(config('aliyun.BucketName'),'MealCateImgs'.$cateData['cate_imgs']);
            $res = MealCate::where('cate_id',$cateid)->update(['cate_imgs'=>$filename,'update_time'=>$nowtime]);
            if($res){
                return response()->json(['success'=>true,'code'=>"0",'message'=>"已保存"]);
            }else{
                return response()->json(['success'=>false,'code'=>'3','message'=>'保存失败请联系开发人员！']);
            }
        }else{
            return response()->json(['success'=>false,'code'=>'2','message'=>'请上传新的餐品图片！']);
        }
    }

    /**
     * 修改信息
     */
    public function editShopInfo(Request $request)
    {
        $action = $request->input('action');
        $file = $request->file('avatar');
        $shopId = $request->input('shop_id');
        $discount = $request->input('discount',0);
        $shopName = $request->input('shop_name');
        $shopAddress = $request->input('shop_address');
        $shop = new MealShop;
        $shopInfo = $shop->where('shop_id',$shopId)->first();
        
        if($request->hasFile('avatar')){
            $dayTime = date('Ymd');
            $file = $request->file('avatar');
            $fileExt = ltrim($file->getClientOriginalExtension(),'.');
            $fileName = md5(uniqid(microtime(true),true)).'.'.$fileExt;
            $filePath = 'MealShopLogo/'.$dayTime.'/';
            $logo = '/'.$dayTime.'/'.$fileName;
            $upRes = OSS::publicUpload(config('aliyun.BucketName'),$filePath.$fileName,$file);
            $shopInfo->shop_logo = $logo;
        }
        if($request->input('resetpass',false) == 1){
            $user = User::where('user_id',$shopInfo->users_id)->first();
            $user->password = strtoupper(md5('123456'));
            $res = $user->save();
        }
        if($request->input('packfood',false) !== false){
            $shopInfo->packfood_if = $request->input('packfood',false);
        }
        if($discount > 0){
            $shopInfo->discount = $discount;
        }
        $shopInfo->shop_name = $shopName;
        $shopInfo->shop_address = $shopAddress;
        if($shopInfo->save()){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'修改成功',
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'006',
                'message'=>'修改出错，请稍后再试！',
            ]);
        }
    }

    /**
     * 修改圈内排序
     */
    public function circleSort(Request $request)
    {
        $shopId = $request->input('shop_id');
        $circle_sort = $request->input('sort');
        $shop = new MealShop;
        $shopInfo = $shop->where('shop_id',$shopId)->first();
        $shopInfo->circle_sort = $circle_sort;
        if($shopInfo->save()){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'修改成功',
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'006',
                'message'=>'修改出错，请稍后再试！',
            ]);
        }
    }

    /**
     * 修改饭店位置
     */
    public function editPosition(Request $request)
    {
        $shopId = $request->input('shop_id');
        $lng = $request->input('lng');
        $lat = $request->input('lat');
        $address = $request->input('address');
        $shop = new MealShop;
        $shopInfo = $shop->where('shop_id',$shopId)->first();
        $shopInfo->shop_position_y = $lng;
        $shopInfo->shop_position_x = $lat;
        $shopInfo->shop_address = $address;
        if($shopInfo->save()){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'修改成功',
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'006',
                'message'=>'修改出错，请稍后再试！',
            ]);
        }
    }

    /**
     * 商圈
     */
    public function circlelist(Request $request)
    {
        $circle = new Circle;

        $circles = $circle->select(DB::raw('*,null as shop_sum,null as user_sum,null as ptuser_num,null as card_num'))->get();
        return view('shop.circle',['circles'=>$circles]);
    }

    /**
     * 获取商圈
     */
    public function getAllCircle(Request $request)
    {
        $circle = new Circle;

        $circles = $circle->select(DB::raw('*,null as shop_sum,null as user_sum,null as ptuser_num,null as card_num'))->get();

        $userposition = User::where([['user_ttp','=',1],['last_position_lat','<>',null]])->select('last_position_lat','last_position_lng')->get();

        return response()->json([
            'success'=>true,
            'code'=>'000',
            'message'=>'完成',
            'data'=>$circles,
            'users'=>$userposition
        ]);
    }

    /**
     * 商圈
     */
    public function circleView(Request $request)
    {
        $circle = new Circle;

        $circles = $circle->select(DB::raw('*,null as shop_sum,null as user_sum,null as ptuser_num,null as card_num'))->get();

        return view('shop.circleview',['circles'=>$circles]);
    }

    /**
     * 添加商圈
     */
    public function circleAdd(Request $request)
    {
        $circle = new Circle;
        return response()->json($circle->add($request->all()));
    }

    /**
     * 删除商圈
     */
    public function delCircle(Request $request)
    {
        $res = Circle::where('circle_id',$request->input('circle_id'))->delete();
        if($res){
            return response()->json([
                'success'=>true,
                'code'=>"000",
                'message'=>"删除成功"
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'006',
                'message'=>'商圈不存在或已删除'
            ]);
        }
    }

    /**
     * 修改饭店商圈
     */
    public function editCircle(Request $request)
    {
        $circle = new Circle;
        $shop = new MealShop;
        $shopinfo = $shop->where('shop_id',$request->input('shop_id'))->first();
        $shopinfo->circle_id = $request->input('circle_id');
        // $circle->
        if($shopinfo->save()){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'修改成功'
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'006',
                'message'=>'修改出错，请稍后再试！'
            ]);
        }
    }

    /**
     * 修改饭店企业
     */
    public function editCompany(Request $request)
    {
        $company = new MealCompany;
        $shop = new MealShop;
        $shopinfo = $shop->whereIn('shop_id',$request->input('shop_id'))->update(['company_id'=>$request->input('company_id')]);
        if($shopinfo){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'修改成功'
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'006',
                'message'=>'修改出错，请稍后再试！'
            ]);
        }
    }

    /**
     * 搜索
     */
    public function circleSearch(Request $request)
    {
        $post = $request->all();
        $circle = new Circle;
        $circles = $circle->where('circle_name','=',$post['circle_name'])->first();
        if($circles){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'查询成功',
                'data'=>$circles
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'004',
                'message'=>'没有这个商圈',
                'data'=>[],
            ]);
        }
    }

    /**
     * 搜索企业
     */
    public function companySearch(Request $request)
    {
        $post = $request->all();
        $company = new MealCompany;
        $companys = $company->where('company_name','=',$post['company_name'])->first();
        if($companys){
            return response()->json([
                'success'=>true,
                'code'=>'000',
                'message'=>'查询成功',
                'data'=>$companys
            ]);
        }else{
            return response()->json([
                'success'=>false,
                'code'=>'004',
                'message'=>'没有这个企业',
                'data'=>[],
            ]);
        }
    }

    /**
     * 提现
     */
    public function takeView(Request $request)
    {
        $companyid = session('company_id');
        $bank = new Bank;
        $info = $bank->where('company_id',$companyid)->first();
        $usersid = MealShop::where('company_id',$companyid)->pluck('users_id');
        $account = User::whereIn('user_id',$usersid)->sum('account');
        return view('shop.tixian',['cardinfo'=>$info,'account'=>$account]);
    }

    /**
     * 提现
     */
    public function takeAction(Request $request) 
    {   
        $validator = Validator::make($request->all(), [
            'amount' => 'required',
            // 'bank_id' => 'required',
            // 'pay_passwd' => 'required',
            'bank_name' => 'required',
            'bank_number' => 'required',
            'bank_tel' => 'required',
            'itsbank' => 'required',
            'openbank' => 'required',
            'desc' => 'max:80',
        ],[
            'amount.required'=>'请填写提取金额！',
            // 'bank_id.required'=>'请选择银行卡！',
            'desc.max'=>'描述不可超过80字！',
            // 'pay_passwd.required'=>'请填写提现密码',
        ]);
        if($validator->fails()){
            $errors = $validator->errors()->all();
            if(count($errors)<=1){
                return response()->json(['success'=>false,'code'=>'003','message'=>$errors[0]]);
            }else{
                return response()->json(['success'=>false,'code'=>'003','message'=>$errors]);
            }
        }
        $company_id = session('company_id');
        $post = $request->only(['amount','bank_id','desc']);
        $bankdata = Bank::find($post['bank_id']);
        if(!$bankdata){
            // return response()->json(['success'=>false,'code'=>'021','message'=>'不存在此银行卡！']);
            $bank = new Bank;
            $addres = $bank->createBankCard([
                'bank_name'=>$request->input('bank_name'),
                'bank_number'=>$request->input('bank_number'),
                'bank_tel'=>$request->input('bank_tel'),
                'itsbank'=>$request->input('itsbank'),
                'openbank'=>$request->input('openbank'),
                'company_id'=>$company_id,
            ],0);
            if(!$addres['success']){
                return response()->json($addres);
            }
            $post['bank_id'] = $addres['data']['bank_id'];
        }
        
        $usersid = MealShop::where('company_id',$company_id)->pluck('users_id');
        if(!$usersid){
            return response()->json(['success'=>false,'code'=>'021','message'=>'您没有店铺']);
        }

        $account = User::whereIn('user_id',$usersid)->sum('account');

        $message = '';
        $post['users_id'] = 0;
        $post['company_id'] = $company_id;
        if($account<$post['amount']){
            return response()->json(['success'=>false,'code'=>'021','message'=>'抱歉，您的余额不足！']);
        }
        // $user->account = $account-$post['amount'];
        $userres = User::whereIn('user_id',$usersid)->update(['account'=>0]);
        if(!$userres){
            return response()->json(['success'=>false,'code'=>'021','message'=>'提交申请失败，请您稍后重试！']);
        }
        $ext = new Extract();
        $post['extract_mouth'] = 1;
        $result = $ext->submitApplic($post);
        if($result){
            return response()->json(['success'=>true,'code'=>'000','message'=>'申请已提交，请您耐心等候！'.$message]);
        }else{
            return response()->json(['success'=>false,'code'=>'021','message'=>'提交申请失败，请您稍后重试！']);
        }
    }
}
